package com.sunjy.common.filter;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.sunjy.common.domain.ApiResponseT;
import com.sunjy.common.domain.HeaderEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

@Slf4j
public class CheckData extends  AbstractAction {


	@Override
	public ApiResponseT<Object> invoke(InvokeContext context) {

		ProceedingJoinPoint pjp = (ProceedingJoinPoint) context.getParam();
		Object[] args=pjp.getArgs();
		for (Object object : args) {
			if (object instanceof BindingResult) {
				BindingResult result = (BindingResult) object;
				if (result.hasErrors()) {
					List<FieldError> errorList = result.getFieldErrors();
					for (FieldError error : errorList) {
						log.info("{},{}", error.getField(), error.getDefaultMessage());
						return ApiResponseT.error(HeaderEnum.PARAM_MSG.getCode(),error.getField() + ":" + error.getDefaultMessage());
					}
				}
			} 
		}
		
		return context.invokeNext();
		
		
	}

}
